﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using Joyintech.Common;
using System.IO;
using System.Text;

namespace Joyintech.Admin.inpiao365.general.createhtml
{
    /// <summary>
    /// 生成首页操作页面
    /// </summary>
    public partial class createIndex : System.Web.UI.Page
    {
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            { 
                //生成首页
                Create();
            }
        }

        /// <summary>
        /// 生成首页
        /// </summary>
        private void Create()
        {
            systemTemplate_BLL bll = new systemTemplate_BLL();

            List<systemTemplate> list = new List<systemTemplate>();

            list = bll.GetModelList("TemplateType='IndexPageTemplate' and IsDefault=1");

            if (list != null && list.Count > 0)
            {
                if (list.Count > 1)
                {
                    MessageBox.Show(this, "默认首页模板只能一个，请重新在模板管理中设置！");
                    return;
                }

                systemTemplate model = list[0];

                //检查模板文件是否存在
                if (!Utils.FileExists(Server.MapPath("../../templates/" + model.RelatedFileName)))
                {
                    MessageBox.Show(this, "首页模板文件不存在，可能已被移除，请确认！");
                    return; 
                }

                VelocityHelper vh = new VelocityHelper();
                vh.Init(@"~/templates");

                //为首页提供所有bll方法
                string bllName = ConfigHelper.GetConfigString("BLL");
                System.Reflection.Assembly BLL = System.Reflection.Assembly.Load(bllName);

                Type[] types = BLL.GetTypes();

                // 遍历程序集中的类
                foreach (Type type in types)
                {
                    string className = type.ToString().Replace(bllName + ".", "");

                    object stu = type.Assembly.CreateInstance(type.ToString());

                    //添加页面操作类BLL
                    vh.Put(className, stu);
                }

                string html = vh.Display(model.RelatedFileName);

                string directory = model.FileCreatePath + "\\";

                string fileName = directory + model.CreatedFileFullName + model.CreatedFileExtName;

                StreamWriter objSw = new StreamWriter(fileName, false, Encoding.GetEncoding(model.TemplateCharset));
                objSw.WriteLine(html);
                objSw.Flush();
                objSw.Close();

                //MessageBox.Show(this, "生成成功！");
                Response.Write("生成成功!");
            }
            else
            {
                //MessageBox.Show(this, "您还没有设置首页模板！");
                Response.Write("您还没有设置首页模板!");
            }
        }
    }
}